package tc.de.krawallmann.twl;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

import de.matthiasmann.twl.ActionMap;
import de.matthiasmann.twl.Widget;

/**
 * The base class for all game states when using AbstractTWLStateBasedGame.
 * <p>
 * To create your UI you can override {@link #createRootPane()} or
 * {@link #init(org.newdawn.slick.GameContainer, org.newdawn.slick.state.StateBasedGame)}
 * .
 * </p>
 * <p>
 * Set the position and size of the widgets inside {@link #layoutRootPane()}.
 * </p>
 * 
 * @author Matthias Mann
 * @author KrawallMann
 */
public abstract class BasicTWLGameState extends BasicGameState {// NOPMD

	/** The root pane. */
	private transient RootPane rootPane;

	/**
	 * Installs the rootPane of this state as the active root pane. Calls
	 * createRootPane() on first run.
	 * 
	 * @param container
	 *            the GameContainer instance
	 * @param game
	 *            the StateBasedGame instance
	 * @throws SlickException
	 *             the slick exception
	 * @see #createRootPane()
	 */
	@Override
	public void enter(final GameContainer container, final StateBasedGame game)
			throws SlickException {
		((AbstractTWLStateBasedGame) game).setRootPane(getRootPane());
	}

	/**
	 * Returns the root pane for this game state. Calls
	 * 
	 * @return the root pane {@link #createRootPane()} if it has not yet been
	 *         created.
	 */
	public RootPane getRootPane() {
		if (rootPane == null) {
			rootPane = createRootPane();
			if (rootPane.getState() != this) {
				throw new IllegalStateException("rootPane.getState() != this");
			}
		}
		return rootPane;
	}

	/**
	 * This method is called when keyboard focus is transfered to a UI widget or
	 * to another application.
	 */
	public void keyboardFocusLost() {// NOPMD
	}
	
	public void reload() {
		setRootPane(createRootPane());
		layoutRootPane();
	}

	/**
	 * This method is called when the layout of the root pane needs to be
	 * updated. Widget position and size should only be changed within this
	 * method.
	 * 
	 * @see Widget#setPosition(int, int)
	 * @see Widget#setSize(int, int)
	 * @see Widget#adjustSize()
	 */
	public void layoutRootPane() {// NOPMD
	}

	/**
	 * Sets the root pane.
	 * 
	 * @param rootPane
	 *            the new root pane
	 */
	public void setRootPane(final RootPane rootPane) {
		this.rootPane = rootPane;
	}

	/**
	 * Override this method to customize the root pane for your UI for this
	 * state.
	 * <p>
	 * The theme name of the RootPane created by this method is "state"+getID().
	 * It will also register all action methods to the rootPane.
	 * </p>
	 * <p>
	 * Do not call this method. Call {@link #getRootPane()} instead
	 * </p>
	 * <p>
	 * When overriding this method don't call {@link #getRootPane()} from within
	 * this method or it will lead to an endless loop.
	 * </p>
	 * 
	 * @return the created root pane
	 * @see ActionMap.Action
	 * @see ActionMap#addMapping(java.lang.Object)
	 * @see Widget#setActionMap(de.matthiasmann.twl.ActionMap)
	 * @see Widget#setTheme(java.lang.String)
	 */
	protected RootPane createRootPane() {
		final RootPane pane = new RootPane(this);
		pane.setTheme("state" + getID());
		pane.getOrCreateActionMap().addMapping(this);
		return pane;
	}
}
